/*******************************************************************************
 * Copyright (c) 2010, 2030 www.mahao.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.eshopmanagement.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.itlaobing.eshopmanagement.entity.Area;
import cn.itlaobing.eshopmanagement.service.AreaService;

/** 
 * ClassName: AreasController <br/> 
 * Function: TODO 功能描述 <br/> 
 * date: 2017年5月23日 上午10:48:47 <br/> 
 * 
 * @author Mmmmm 
 * @version  
 * @since JDK 1.8 
 */
@Controller()
@RequestMapping("/areas")
public class AreasController extends BaseController {

	@Autowired
	private AreaService areaService;
	
	@RequestMapping()
	public String index(Model model) {
		List<Area> list= areaService.selectWithPage(buildPageRequestSearchFilter());
		model.addAttribute("areas", list);
		return "/areas/index";
	}
	
	/**
	 * _new:(新建--获取表单). <br/>
	 * 
	 * @author Mmmmm
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String _new(Model model) {
		Area area = new Area();
		model.addAttribute("area", area);
		return "/areas/create";
	}

	/**
	 * create:(新增--提交数据). <br/>
	 * 
	 * @author Mmmmm
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@Valid @ModelAttribute Area area
			, BindingResult result, Model model,
			RedirectAttributes redirectAttribute) {
		if (result.hasErrors()) {
			return "/areas/create";
		}
		areaService.addArea(area);
		redirectAttribute.addFlashAttribute("toastMsg", "保存" + area.getAreaName() + "成功！");
		return "redirect:/areas";
	}

	/*
	 * 查询
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String show(@PathVariable Long id, Model model) {
		model.addAttribute("area", areaService.selectById(id));
		return "areas/show";
	}

	/**
	 * edit:(修改---获取表单). <br/>
	 * 
	 * @author Mmmmm
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String edit(@PathVariable Long id, Model model) {
		Area area = areaService.selectById(id);
		model.addAttribute("area", area);
		return "areas/update";
	}

	/**
	 * edit:(修改---提交表单). <br/>
	 * 
	 * @author Mmmmm
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(@PathVariable Long id
			, @Valid @ModelAttribute Area area
			, BindingResult result,
			Model model, RedirectAttributes redirectAttribute) {
		if (result.hasErrors()) {
			return "/areas/update";
		}
		area.setId(id);
		;
		areaService.update(area);
		redirectAttribute.addFlashAttribute("toastMsg", "更新" + area.getAreaName() + "成功！");
		return "redirect:/areas";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Area destroy(@PathVariable Long id) {
		Area area = areaService.selectById(id);
		areaService.deleteById(id);
		return area;
	}
}
